package cn.ekuma.epos.businesslogic;

import java.util.Date;
import java.util.List;

import net.sourceforge.jtimepiece.TimeUtil;
import cn.ekuma.epos.datalogic.I_DataLogicSales;

import com.openbravo.bean.Service;
import com.openbravo.bean.ServiceCalendarDayLine;
import com.openbravo.bean.ServiceCalendarWeakLine;
import com.openbravo.bean.ServiceDayState;
import com.openbravo.bean.Serving;
import com.openbravo.data.basic.BasicException;
import com.openbravo.pos.util.RoundUtils;

public class SerivceUtil {

	 public static boolean isServiceAble(I_DataLogicSales dlSales,String serviceId,Date start,Date end) throws BasicException{
	        List<ServiceCalendarDayLine> serviceDayLine=dlSales.listServiceCalendarDayLineByService(serviceId);
	        List<ServiceCalendarWeakLine> serviceWeakLine=dlSales.listServiceCalendarWeakLineByService(serviceId);
	        List<ServiceDayState> serviceDayState=dlSales.list(ServiceDayState.class, Service.class,serviceId);
	        return contains(serviceDayLine,serviceWeakLine,serviceDayState,start,end==null?new Date():end);
	    }


	    public static boolean contains(List<ServiceCalendarDayLine> serviceDayLine,List<ServiceCalendarWeakLine> serviceWeakLine, List<ServiceDayState> serviceDayState,Date start,Date end){
	         for(ServiceDayState dayState:serviceDayState){
	            if(isInDate(start,end,dayState.getStart(),dayState.getEnd())){
	                return dayState.isAble();
	            }else if(!dayState.isAble()&&isInDate(dayState.getStart(),dayState.getEnd(),start,end)){
	                return false;
	            }else if(dayState.isAble()&&isBetween(dayState.getStart(),dayState.getEnd(),start)){
	                return contains(serviceDayLine,serviceWeakLine,serviceDayState,dayState.getEnd(),end);
	            }
	        }
	        for(ServiceCalendarDayLine dayLine:serviceDayLine){
	            if(isInDate(start,end,dayLine.getStart(),dayLine.getEnd())){
	                return dayLine.isAble();
	            }else if(!dayLine.isAble()&&isInDate(dayLine.getStart(),dayLine.getEnd(),start,end)){
	                return false;
	            }else if(dayLine.isAble()&&isBetween(dayLine.getStart(),dayLine.getEnd(),start)){
	                return contains(serviceDayLine,serviceWeakLine,serviceDayState,dayLine.getEnd(),end);
	            }
	        }
	        int dayofweak=TimeUtil.getDayOfWeek(start);
	        int days=TimeUtil.getCalendarDaysAfter(start, end);
	        if(days!=0){
	            for(ServiceCalendarWeakLine weakLine:serviceWeakLine){
	                if(weakLine.getDayInWeak()==dayofweak){
	                    Date weakDayStart=TimeUtil.GetDateForNewTime(start,weakLine.getStart());
	                    Date weakDayEnd=TimeUtil.GetDateForNewTime(start,weakLine.getEnd());
	                    if(isBetween(weakDayStart,weakDayEnd,start)){
	                        return contains(serviceDayLine,serviceWeakLine,serviceDayState,weakDayEnd,end);
	                    }
	                }
	            }
	        }else{
	            for(ServiceCalendarWeakLine weakLine:serviceWeakLine){
	                if(weakLine.getDayInWeak()==dayofweak){
	                     Date weakDayStart=TimeUtil.GetDateForNewTime(start,weakLine.getStart());
	                     Date weakDayEnd=TimeUtil.GetDateForNewTime(start,weakLine.getEnd());
	                     if(isInDate(weakDayStart,weakDayEnd,start,end))
	                         return true;
	                     else if(isBetween(weakDayStart,weakDayEnd,start))
	                         return contains(serviceDayLine,serviceWeakLine,serviceDayState,weakDayEnd,end);
	                }
	            }
	        }
	        return false;
	    }

	    public static boolean isBetween(Date start,Date end,Date in){
	        return TimeUtil.isAfter(in, start)&&TimeUtil.isBefore(in, end);
	    }

	    public static boolean isInDate(Date start,Date end,Date start1,Date end1){
	        return TimeUtil.isAfter(start1, start)&&TimeUtil.isBefore(end1, end);
	    }
	    
	    
	    public static int calcSubDays(Serving serving){
            return calcSubDays(serving.getRantCycleStyle(),serving.getRantData(),serving.getReturnData());
    }

    public static int calcSubDays(String rantCycleStyle,Date start,Date end){
        int exemptDate = 0;
            Date returndate = end;
            if(returndate ==null)
                returndate=new Date();
            int subDays = 0;
            int rangDays = 0;
            if(Service.RANTCYCLESTYLE_everyTime.equalsIgnoreCase(rantCycleStyle)){
                subDays=1;
            }else if (Service.RANTCYCLESTYLE_everyDay.equalsIgnoreCase(rantCycleStyle)) {
                rangDays = TimeUtil.getCalendarDaysAfter(start,returndate);
                subDays = rangDays - exemptDate;
            } else if (Service.RANTCYCLESTYLE_everyWeek.equalsIgnoreCase(rantCycleStyle)) {
            } else if (Service.RANTCYCLESTYLE_moon.equalsIgnoreCase(rantCycleStyle)) {
                rangDays = TimeUtil.getCalendarMonthsAfter(start, returndate);
                subDays = rangDays - exemptDate;
            } else if (Service.RANTCYCLESTYLE_year.equalsIgnoreCase(rantCycleStyle)) {
                rangDays = TimeUtil.getCalendarYearsAfter(start, returndate);
                subDays = rangDays - exemptDate;
            }
            return subDays;
    }

    public static double calcSubTotal(Serving serving) {
            double subTotal = 0;
            subTotal = RoundUtils.multiply(calcSubDays(serving) , serving.getCyclePrice());
            return subTotal;
      }
	
}
